<?php

namespace app\admin\controller\vppz;

use think\Session;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Qxzl extends Base
{

    /**
     * Service模型对象
     * @var \app\admin\model\vppz\Qxzl
     */
    protected $model = null;

	protected $searchFields = ['id','name','type'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\vppz\Qxzl;
        $this->adminModel = new \app\admin\model\Admin;
        $this->companyModel = new \app\admin\model\vppz\Company;
        $this->authGroupAccessModel = new \app\admin\model\AuthGroupAccess;
        $this->view->assign("stypeList", $this->model->getStypeList());

		// 给数据附加当前appid，appid由Base负责解析
        // 添加租赁销售类型
        $this->assign("saleTypes", [
            'rent' => '租赁',
            'sale' => '销售'
        ]);
    }
 public function __get($name)
    {
        return Session::get('admin.' . $name);
    }


    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
/**
     * 查看
     */
    public function index()
    {
        $ids = input('ids',0);
        $admin = Session::get("admin");
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();


            $list = $this->model
                    ->with(['company'])
                    ->where($where);
            if($admin['is_pingtai'] == 2){
                $company = $this->companyModel->where(['admin_mobile'=>$admin['username']])->find();
               $list = $list->where('company_id','=',$company['id']);
            }else{
                $list = $list->where('company_id','=',$ids);
            }
            $list = $list->order($sort, $order)
                    ->paginate($limit);
            foreach ($list as $row) {

                $row->getRelation('company')->visible(['name']);
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
		$this->assignconfig('company_id',$ids);

        $this->view->assign('company_id',$ids);
        $this->view->assign('admin',$admin);
        return $this->view->fetch();
    }
public function add()
   {
       $ids = input('ids',0);
        if(empty($ids)){
            $admin = Session::get("admin");
            $company = $this->companyModel->where(['admin_mobile'=>$admin['username']])->find();
            $ids = $company['id']??'';
        }
        if(empty($ids)){
             $this->error(__('Parameter %s can not be empty', 'company_id'));
        }
		if ($this->request->isPost()) {
            $this->modelValidate = true;
            $params = $this->request->post('row/a');
            if (empty($params)) {
                $this->error(__('Parameter %s can not be empty', ''));
            }
            $form = $this->preExcludeFields($params);
            if(empty($form['price']) && empty($form['saleprice'])){
                $this->error(__('Parameter %s can not be empty', ''));
            }
            $result = false;
            try {
//                if(empty($form['company_id'])){
//                    $this->error(__('Parameter %s can not be empty', 'company_id'));
//                }

                $data = array(
                    'company_id'=>$ids,
                    'name'=>$form['name'],
                    'type'=>$form['type'],
                    'content'=>$form['content'],
                    'price'=>$form['price'],
                    'yajin'=>$form['yajin'],
                    'saleprice'=>$form['saleprice'],
                    'desc'=>$form['desc'],
                    'image'=>$form['image'],
                    'weigh'=>$form['weigh'],
                    'saletype'=>implode(',',$form['saletype']),
                    'model'=>$form['model'],
                    'stock'=>$form['stock'],
                    'createtime'=>time(),
                    'updatetime'=>time(),
                );
                $this->model->save($data);
                $this->success();
            } catch (ValidateException|PDOException|Exception $e) {
                $this->error($e->getMessage());
            }
        }
        $this->view->assign('company_id',$ids);
        return parent::add();
	}

public function edit($ids = null)
   {
       $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
		if ($this->request->isPost()) {
            $this->modelValidate = true;
            $params = $this->request->post('row/a');
            if (empty($params)) {
                $this->error(__('Parameter %s can not be empty', ''));
            }
            $form = $this->preExcludeFields($params);
            if(empty($form['price']) && empty($form['saleprice'])){
                $this->error(__('Parameter %s can not be empty', ''));
            }
            $result = false;
            try {
//                if(empty($form['company_id'])){
//                    $this->error(__('Parameter %s can not be empty', 'company_id'));
//                }
                $data = array(
                    'name'=>$form['name'],
                    'type'=>$form['type'],
                    'content'=>$form['content'],
                    'price'=>$form['price'],
                    'yajin'=>$form['yajin'],
                    'saleprice'=>$form['saleprice'],
                    'desc'=>$form['desc'],
                    'image'=>$form['image'],
                    'weigh'=>$form['weigh'],
                    'saletype'=>implode(',',$form['saletype']),
                    'model'=>$form['model'],
                    'stock'=>$form['stock'],
                    'updatetime'=>time(),
                );
                $ret=$this->model->where('id','=',$row['id'])->update($data);
                if(empty($ret)){
					$this->error('服务器刚刚走神了，重试看看呢');
				}
                $this->success();
            } catch (ValidateException|PDOException|Exception $e) {
                $this->error($e->getMessage());
            }
        }else{
            $this->view->assign('row', $row);
            return $this->view->fetch();
        }
        return parent::edit();
	}
}
